<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <in label="模糊搜索" prop="searchValue" v-model="queryParams.searchValue" clearable @input="handleQuery"></in>

      <in label="部门id" prop="deptId" v-model="deptId" placeholder="部门id" clearable @input="handleQuery"></in>


      <in label="用户id" prop="userId" v-model="userId" placeholder="用户id" clearable @input="handleQuery"></in>


      <in label="appid" prop="appid" v-model="appid" placeholder="appid" clearable @input="handleQuery"></in>


      <in label="用户id" prop="uid" v-model="uid" placeholder="用户id" clearable @input="handleQuery"></in>


      <el-form-item label="入驻类型" prop="enterType">
        <el-select v-model="queryParams.enterType" placeholder="请选择入驻类型" clearable>
          <el-option
            v-for="dict in dict.type.usr_enter_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <in label="店铺名称" prop="shopName" v-model="shopName" placeholder="店铺名称" clearable @input="handleQuery"></in>


      <in label="店铺标签" prop="label" v-model="label" placeholder="店铺标签" clearable @input="handleQuery"></in>


      <in label="地址" prop="address" v-model="address" placeholder="地址" clearable @input="handleQuery"></in>


      <in label="电话" prop="tel" v-model="tel" placeholder="电话" clearable @input="handleQuery"></in>


      <in label="经度" prop="longitude" v-model="longitude" placeholder="经度" clearable @input="handleQuery"></in>


      <in label="维度" prop="latitude" v-model="latitude" placeholder="维度" clearable @input="handleQuery"></in>


      <in label="配送费" prop="sendFee" v-model="sendFee" placeholder="配送费" clearable @input="handleQuery"></in>


      <in label="配送时间" prop="sendTime" v-model="sendTime" placeholder="配送时间" clearable @input="handleQuery"></in>


      <in label="店铺简介" prop="intro" v-model="intro" placeholder="店铺简介" clearable @input="handleQuery"></in>


      <in label="公告" prop="notice" v-model="notice" placeholder="公告" clearable @input="handleQuery"></in>


      <in label="客服id" prop="kfs" v-model="kfs" placeholder="客服id" clearable @input="handleQuery"></in>


      <in label="评分" prop="score" v-model="score" placeholder="评分" clearable @input="handleQuery"></in>


      <in label="评价数" prop="commentNum" v-model="commentNum" placeholder="评价数" clearable @input="handleQuery"></in>


      <in label="月售" prop="monthSale" v-model="monthSale" placeholder="月售" clearable @input="handleQuery"></in>


      <in label="总销" prop="totalSale" v-model="totalSale" placeholder="总销" clearable @input="handleQuery"></in>


      <in label="权重" prop="weight" v-model="weight" placeholder="权重" clearable @input="handleQuery"></in>


      <in label="抽成比例" prop="ratio" v-model="ratio" placeholder="抽成比例" clearable @input="handleQuery"></in>


      <el-form-item label="是否下架" prop="isPutaway">
        <el-select v-model="queryParams.isPutaway" placeholder="请选择是否下架" clearable>
          <el-option
            v-for="dict in dict.type.usr_logic"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <in label="下架原因" prop="putawayReason" v-model="putawayReason" placeholder="下架原因" clearable
          @input="handleQuery"></in>


      <in label="下架处理用户id" prop="putawayUid" v-model="putawayUid" placeholder="下架处理用户id" clearable
          @input="handleQuery"></in>


      <el-form-item label="下架时间" prop="putawayTime">
        <el-date-picker clearable
                        v-model="queryParams.putawayTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下架时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:shop:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:shop:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:shop:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:shop:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id" v-if="true"/>
      <el-table-column label="部门id" align="center" prop="deptId"/>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="appid" align="center" prop="appid"/>
      <el-table-column label="用户id" align="center" prop="uid"/>
      <el-table-column label="入驻类型" align="center" prop="enterType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.usr_enter_type" :value="scope.row.enterType"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="shopName"/>
      <el-table-column label="店铺logo" align="center" prop="shopLogo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shopLogo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺背景图" align="center" prop="shopBg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shopBg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺环境图" align="center" prop="imgs" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgs" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺标签" align="center" prop="label"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="电话" align="center" prop="tel"/>
      <el-table-column label="经度" align="center" prop="longitude"/>
      <el-table-column label="维度" align="center" prop="latitude"/>
      <el-table-column label="配送费" align="center" prop="sendFee"/>
      <el-table-column label="配送时间" align="center" prop="sendTime"/>
      <el-table-column label="店铺简介" align="center" prop="intro"/>
      <el-table-column label="公告" align="center" prop="notice"/>
      <el-table-column label="客服id" align="center" prop="kfs"/>
      <el-table-column label="评分" align="center" prop="score"/>
      <el-table-column label="评价数" align="center" prop="commentNum"/>
      <el-table-column label="月售" align="center" prop="monthSale"/>
      <el-table-column label="总销" align="center" prop="totalSale"/>
      <el-table-column label="权重" align="center" prop="weight"/>
      <el-table-column label="抽成比例" align="center" prop="ratio"/>
      <el-table-column label="是否下架" align="center" prop="isPutaway">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.usr_logic" :value="scope.row.isPutaway"/>
        </template>
      </el-table-column>
      <el-table-column label="下架原因" align="center" prop="putawayReason"/>
      <el-table-column label="下架处理用户id" align="center" prop="putawayUid"/>
      <el-table-column label="下架时间" align="center" prop="putawayTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.putawayTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:shop:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:shop:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <in label="部门id" prop="deptId" :span="12" v-model="form.deptId" placeholder="部门id"></in>
        <in label="用户id" prop="userId" :span="12" v-model="form.userId" placeholder="用户id"></in>
        <in label="appid" prop="appid" :span="12" v-model="form.appid" placeholder="appid"></in>
        <in label="用户id" prop="uid" :span="12" v-model="form.uid" placeholder="用户id"></in>
        <el-form-item label="入驻类型" prop="enterType">
          <el-select v-model="form.enterType" placeholder="请选择入驻类型">
            <el-option
              v-for="dict in dict.type.usr_enter_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <in label="店铺名称" prop="shopName" :span="12" v-model="form.shopName" placeholder="店铺名称"></in>
        <el-form-item label="店铺logo">
          <image-upload v-model="form.shopLogo"/>
        </el-form-item>
        <el-form-item label="店铺背景图">
          <image-upload v-model="form.shopBg"/>
        </el-form-item>
        <el-form-item label="店铺环境图">
          <image-upload v-model="form.imgs"/>
        </el-form-item>
        <in label="店铺标签" prop="label" :span="12" v-model="form.label" placeholder="店铺标签"></in>
        <in label="地址" prop="address" :span="12" v-model="form.address" placeholder="地址"></in>
        <in label="电话" prop="tel" :span="12" v-model="form.tel" placeholder="电话"></in>
        <in label="经度" prop="longitude" :span="12" v-model="form.longitude" placeholder="经度"></in>
        <in label="维度" prop="latitude" :span="12" v-model="form.latitude" placeholder="维度"></in>
        <in label="配送费" prop="sendFee" :span="12" v-model="form.sendFee" placeholder="配送费"></in>
        <in label="配送时间" prop="sendTime" :span="12" v-model="form.sendTime" placeholder="配送时间"></in>
        <el-form-item label="店铺简介" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <in label="公告" prop="notice" :span="12" v-model="form.notice" placeholder="公告"></in>
        <in label="客服id" prop="kfs" :span="12" v-model="form.kfs" placeholder="客服id"></in>
        <in label="评分" prop="score" :span="12" v-model="form.score" placeholder="评分"></in>
        <in label="评价数" prop="commentNum" :span="12" v-model="form.commentNum" placeholder="评价数"></in>
        <in label="月售" prop="monthSale" :span="12" v-model="form.monthSale" placeholder="月售"></in>
        <in label="总销" prop="totalSale" :span="12" v-model="form.totalSale" placeholder="总销"></in>
        <in label="权重" prop="weight" :span="12" v-model="form.weight" placeholder="权重"></in>
        <in label="抽成比例" prop="ratio" :span="12" v-model="form.ratio" placeholder="抽成比例"></in>
        <el-form-item label="是否下架" prop="isPutaway">
          <el-select v-model="form.isPutaway" placeholder="请选择是否下架">
            <el-option
              v-for="dict in dict.type.usr_logic"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下架原因" prop="putawayReason">
          <el-input v-model="form.putawayReason" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <in label="下架处理用户id" prop="putawayUid" :span="12" v-model="form.putawayUid" placeholder="下架处理用户id"></in>
        <el-form-item label="下架时间" prop="putawayTime">
          <el-date-picker clearable
                          v-model="form.putawayTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择下架时间">
          </el-date-picker>
        </el-form-item>
        <in label="备注" prop="remark" :span="12" v-model="form.remark" placeholder="备注"></in>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listShop, getShop, delShop, addShop, updateShop} from "@/api/mall/shop";

  export default {
    name: "Shop",
    dicts: ['usr_enter_type', 'usr_logic'],
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 店铺信息表格数据
        shopList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          searchValue: undefined,
          deptId: undefined,

          userId: undefined,

          appid: undefined,

          uid: undefined,

          enterType: undefined,

          shopName: undefined,

          shopLogo: undefined,

          shopBg: undefined,

          imgs: undefined,

          label: undefined,

          address: undefined,

          tel: undefined,

          longitude: undefined,

          latitude: undefined,

          sendFee: undefined,

          sendTime: undefined,

          intro: undefined,

          notice: undefined,

          kfs: undefined,

          score: undefined,

          commentNum: undefined,

          monthSale: undefined,

          totalSale: undefined,

          weight: undefined,

          ratio: undefined,

          isPutaway: undefined,

          putawayReason: undefined,

          putawayUid: undefined,

          putawayTime: undefined,

        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          id: [
            {required: true, message: "id不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询店铺信息列表 */
      getList() {
        this.loading = true;
        listShop(this.queryParams).then(response => {
          this.shopList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          searchValue: undefined,
          id: undefined,
          deptId: undefined,
          userId: undefined,
          appid: undefined,
          uid: undefined,
          enterType: undefined,
          shopName: undefined,
          shopLogo: undefined,
          shopBg: undefined,
          imgs: undefined,
          label: undefined,
          address: undefined,
          tel: undefined,
          longitude: undefined,
          latitude: undefined,
          sendFee: undefined,
          sendTime: undefined,
          intro: undefined,
          notice: undefined,
          kfs: undefined,
          score: undefined,
          commentNum: undefined,
          monthSale: undefined,
          totalSale: undefined,
          weight: undefined,
          ratio: undefined,
          isPutaway: undefined,
          putawayReason: undefined,
          putawayUid: undefined,
          putawayTime: undefined,
          createTime: undefined,
          updateTime: undefined,
          delFlag: undefined,
          createBy: undefined,
          updateBy: undefined,
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加店铺信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getShop(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改店铺信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateShop(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addShop(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除店铺信息编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delShop(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('mall/shop/export', {
          ...this.queryParams
        }, `shop_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
